package com.gxuwz.bignews;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.gxuwz.bignews.fragments.HomeFragment;
import com.gxuwz.bignews.fragments.LoginFragment;
import com.gxuwz.bignews.fragments.RegisterFragment;
import com.gxuwz.bignews.fragments.UserCenterFragment;
import com.gxuwz.bignews.model.util.NotificationUtil;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private BottomNavigationView navView;
    private boolean isLoggedIn = false; // 控制登录状态

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 初始化通知渠道
        NotificationUtil.createNotificationChannel(this);

        navView = findViewById(R.id.nav_view);
        navView.setOnNavigationItemSelectedListener(this);

        // 检查是否有广告页传递的fragment参数
        Intent intent = getIntent();
        String showFragment = intent.getStringExtra("show_fragment");
        if (showFragment != null) {
            if (showFragment.equals("register")) {
                loadFragment(new RegisterFragment());
                navView.setVisibility(BottomNavigationView.GONE);
                return;
            } else if (showFragment.equals("login")) {
                loadFragment(new LoginFragment());
                navView.setVisibility(BottomNavigationView.GONE);
                return;
            }
        }

        // 检查本地是否有已登录用户信息
        SharedPreferences sp = getSharedPreferences("user_info", MODE_PRIVATE);
        String phone = sp.getString("phone", null);
        String username = sp.getString("username", null);
        if (phone != null && username != null) {
            isLoggedIn = true;
        }

        // 检查登录状态
        if (isLoggedIn) {
            // 已登录，默认显示新闻列表
            loadFragment(new HomeFragment());
            navView.setVisibility(BottomNavigationView.VISIBLE);
        } else {
            // 未登录，显示登录页面
            loadFragment(new LoginFragment());
            // 隐藏底部导航栏
            navView.setVisibility(BottomNavigationView.GONE);
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment fragment = null;
        int itemId = item.getItemId();

        if (itemId == R.id.navigation_news) {
            fragment = new HomeFragment();
        } else if (itemId == R.id.navigation_user) {
            fragment = new UserCenterFragment();
        }

        return loadFragment(fragment);
    }

    // 加载Fragment的方法
    private boolean loadFragment(Fragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_container, fragment)
                    .commit();
            return true;
        }
        return false;
    }

    // 登录成功后调用此方法
    public void onLoginSuccess() {
        isLoggedIn = true;
        navView.setVisibility(BottomNavigationView.VISIBLE);
        navView.setSelectedItemId(R.id.navigation_news);
    }
}